<?php
namespace app\jobmanagement\controller;

use app\constdir\ApiConst;
use app\jobmanagement\service\Product as sProduct;
class Product extends Base
{
    private $sProduct;
    public function __construct(){
        parent::__construct();
        $this->sProduct = new sProduct();
    }


    /**
     * @introduction:产品的新增与编辑
     * @return \think\Collection|\think\model\Collection
     * @author:cxh
     * @date: 2020/5/20 11:44
     */
    public function productSave(){
        $param = [
            'type' => input('type'),
            'id' => input('id',0),
            'name' => input('name'),
            'amount' => input('amount'),
            'unit' => input('unit'),
            'work' => input('work'),
        ];

        if (!in_array($param['type'] , [1,2])) {
            return $this->apiError(ApiConst::PRODUCT_SAVE_TYPE_ERROR_MSG,ApiConst::PRODUCT_SAVE_TYPE_ERROR_CODE);
        }

        if ($param['type'] == 1 and $param['id'] > 0) {
            return $this->apiError(ApiConst::PRODUCT_ADD_TYPE_ID_ERROR_MSG,ApiConst::PRODUCT_ADD_TYPE_ID_ERROR_CODE);
        }

        if ($param['type'] == 2 and $param['id'] < 1) {
            return $this->apiError(ApiConst::PRODUCT_EDIT_TYPE_ID_ERROR_MSG,ApiConst::PRODUCT_EDIT_TYPE_ID_ERROR_CODE);
        }

        if (empty($param['name'])) {
            return $this->apiError(ApiConst::PRODUCT_SAVE_NAME_ERROR_MSG,ApiConst::PRODUCT_SAVE_NAME_ERROR_CODE);
        }

        if (empty($param['amount'])) {
            return $this->apiError(ApiConst::PRODUCT_SAVE_AMOUNT_ERROR_MSG,ApiConst::PRODUCT_SAVE_AMOUNT_ERROR_CODE);
        }

        $amount = (float)$param['amount'];
        // 数据格式是否正确
        $temp = explode ( '.', $amount);
        if (sizeof ( $temp ) > 1) {
            $decimal = end ( $temp );
            if (strlen($decimal) > 2) {
                return $this->apiError(ApiConst::PRODUCT_AMOUNT_ERROR_MSG,ApiConst::PRODUCT_AMOUNT_ERROR_CODE);
            }
        }

        if (empty($param['unit'])) {
            return $this->apiError(ApiConst::PRODUCT_SAVE_UNIT_ERROR_MSG,ApiConst::PRODUCT_SAVE_UNIT_ERROR_CODE);
        }

        if (empty($param['work'])) {
            return $this->apiError(ApiConst::PRODUCT_SAVE_WORK_ERROR_MSG,ApiConst::PRODUCT_SAVE_WORK_ERROR_CODE);
        }

        $prodSave =  $this->sProduct->productSave($param);

        return $this->apiSuccessError($prodSave);

    }


    /**
     * @introduction:产品新增或者编辑所需内容视图页输出
     * @return \think\Collection|\think\model\Collection
     * @author:cxh
     * @date: 2020/5/20 14:47
     */
    public function productEdit(){
        $param = [
            'type' => input('type'),
            'id' => input('id',0),
        ];

        if (!in_array($param['type'] , [1,2])) {
            return $this->apiError(ApiConst::PRODUCT_VIEW_TYPE_ERROR_MSG,ApiConst::PRODUCT_VIEW_TYPE_ERROR_CODE);
        }

        if ($param['type'] == 1 and $param['id'] > 0) {
            return $this->apiError(ApiConst::PRODUCT_ADD_TYPE_ID_ERROR_MSG,ApiConst::PRODUCT_ADD_TYPE_ID_ERROR_CODE);
        }

        if ($param['type'] == 2 and $param['id'] < 1) {
            return $this->apiError(ApiConst::PRODUCT_EDIT_TYPE_ID_ERROR_MSG,ApiConst::PRODUCT_EDIT_TYPE_ID_ERROR_CODE);
        }

        $prodEdit =  $this->sProduct->productEdit($param);

        return $this->apiSuccessError($prodEdit);
    }


    /**
     * @introduction:根据产品ID查询出详细信息以及工种信息
     * @return \think\Collection|\think\model\Collection
     * @author:cxh
     * @date: 2020/5/20 16:28
     */
    public function productInfo(){
        $param = [
            'id' => input('id',0),
        ];

        if ($param['id'] < 1) {
            return $this->apiError(ApiConst::PRODUCT_INFO_ID_ERROR_MSG,ApiConst::PRODUCT_INFO_ID_ERROR_CODE);
        }

        $productInfo =  $this->sProduct->productInfo($param);

        return $this->apiSuccessError($productInfo);

    }

    /**
     * @introduction:根据分页获取对应用户的产品列表
     * @return \think\Collection|\think\model\Collection
     * @author:cxh
     * @date: 2020/5/20 16:17
     */
    public function productList(){
        $param = [
            'page' => input('page',1),
        ];

        if ($param['page'] < 1) {
            return $this->apiError(ApiConst::PRODUCT_LIST_PAGE_ERROR_MSG,ApiConst::PRODUCT_LIST_PAGE_ERROR_CODE);
        }

        $productList =  $this->sProduct->productList($param);

        return $this->apiSuccessError($productList);

    }


    /**
     * @introduction:.根据参数产品ID去删除对应的产品
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/5/23 16:41
     */
    public function productDelete(){
        $param = [
            'id' => (int)input('id',0),
        ];

        if (empty($param['id'])) {
            return $this->apiError(ApiConst::PRODUCT_DELETE_ID_ERROR_MSG,ApiConst::PRODUCT_DELETE_ID_ERROR_CODE);
        }

        $productList =  $this->sProduct->productDelete($param);

        return $this->apiSuccessError($productList);

    }






}
